package wang.unclecat.mylibrary.multidevicekit;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import ohos.bundle.IBundleManager;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.data.distributed.device.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.List;

import static ohos.security.SystemPermission.DISTRIBUTED_DATASYNC;

/**
 * 多设备通信帮助类
 *
 * @author: 喵叔catuncle
 */
public class MultiDeviceHelper {

    /**
     * 启动远程ability(同设备)
     */
    public static void openRemoteAbility(Ability context, String bundleName, String abilityName) {
        Intent intent = new Intent();

        Operation operation = new Intent.OperationBuilder()
                .withBundleName(bundleName)
                .withAbilityName(abilityName)
                .build();
        intent.setOperation(operation);
        context.startAbility(intent);
    }

    /**
     * 打开远程Ability(跨设备)
     */
    public static void openRemoteAbility(Context context, String deviceId, String bundleName, String abilityName) {
        Intent intent = new Intent();
        openRemoteAbility(context, deviceId, bundleName, abilityName, intent);
    }

    public static void openRemoteAbility(Context context, String deviceId, String bundleName, String abilityName, Intent intent) {
        if (context.verifySelfPermission(DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
            System.err.println("no permission!!!");
            return;
        }

        Operation operation = new Intent.OperationBuilder()
                .withDeviceId(deviceId)
                .withBundleName(bundleName)
                .withAbilityName(abilityName)
                .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                .build();

        intent.setOperation(operation);

        context.startAbility(intent, 0);
    }

    /**
     * 连接远程Service(同设备)
     */
    public static void connectService(Context context, String bundleName, String serviceName, IAbilityConnection conn) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(bundleName)
                .withAbilityName(serviceName)
                .build();
        intent.setOperation(operation);

        context.connectAbility(intent, conn);
    }

    /**
     * 连接远程Service
     */
    public static void connectService(Context context, String deviceId, String bundleName, String serviceName, IAbilityConnection conn) {
        if (deviceId != null) {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId(deviceId)
                    .withBundleName(bundleName)
                    .withAbilityName(serviceName)
                    .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                    .build();
            intent.setOperation(operation);

            context.connectAbility(intent, conn);
        }
    }

    /**
     * 获取本地设备信息(id name type等)
     */
    public static DeviceInfo getLocalDeviceInfo(Context context) {
        return KvManagerFactory.getInstance()
                .createKvManager(new KvManagerConfig(context)).getLocalDeviceInfo();
    }

    /**
     * 获取online的remote设备信息(id name type等)
     */
    public static List<ohos.distributedschedule.interwork.DeviceInfo> getOnlineDeviceList() {
        return DeviceManager.getDeviceList(ohos.distributedschedule.interwork.DeviceInfo.FLAG_GET_ONLINE_DEVICE);
    }

}
